package com.atguigu.day4;

import java.util.concurrent.*;

public class MyThreadPool {
    public static void main(String[] args) {
        //newFixed(固定线程数)5个工作线程
//        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        //newSingle一个线程
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        //N个线程
//        ExecutorService threadPool = Executors.newCachedThreadPool();

        System.out.println(Runtime.getRuntime().availableProcessors());

        //自定义线程池
        ExecutorService threadPool = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors() * 2,
                2L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()//直接抛异常
//                new ThreadPoolExecutor.CallerRunsPolicy()//将任务回退给调用者
//                new ThreadPoolExecutor.DiscardPolicy()//直接丢弃任务
//                 new ThreadPoolExecutor.DiscardOldestPolicy()//丢弃等待最久的任务
        );


        //20个请求线程
        for (int i = 1; i <= 8; i++) {
            int finalI = i;
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "正在受理第" + finalI + "号顾客");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
        threadPool.shutdown();

    }
}
